#define LOG_TAG     "data_cache"
#include <COMMON_HEADER/common_header.h>
/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-03-23     18782       the first version
 */

#define DTU_DATA_CACHE_FILE_PATH  "/data"
#define DTU_DATA_CACHE_FILE_NAME1 "/data/cache1.data"
#define DTU_DATA_CACHE_FILE_NAME2 "/data/cache2.data"

uint8_t data_cache_flag = 1;

//写单条数据缓存写函数
uint8_t data_cache_write(char *buf)
{
    int data_cache_fd = -1;
    char data_cache[2058] = {0};

    data_cache[0] = (sizeof(buf)) & 0xff;
    data_cache[1] = ((sizeof(buf)) & 0xff00) >> 8;

    for(int i=0;i<sizeof(buf);i++)
    {
        data_cache[2+i] = buf[i];
    }

    //检查日志路径是否存在
    if(access(DTU_DATA_CACHE_FILE_PATH, 0))
    {
        //不存在就建立日志路径
        mkdir(DTU_DATA_CACHE_FILE_PATH, 0x777);
    }

    if(data_cache_flag == 1)
    {
        data_cache_fd = open(DTU_DATA_CACHE_FILE_NAME1, O_CREAT | O_RDWR | O_APPEND);
    }
    else {
        data_cache_fd = open(DTU_DATA_CACHE_FILE_NAME2, O_CREAT | O_RDWR | O_APPEND);
    }

    if(data_cache_fd == -1)
    {
       LOG_E("open data cache file failed!");
       return 1;
    }
    else
    {
        write(data_cache_fd,data_cache,sizeof(data_cache));
        fsync(data_cache_fd);
    }
    LOG_I("data cache success");
    close(data_cache_fd);
    return 0;
}

//读单条缓存数据
char *data_cache_read(void)
{
    char *data_cache = NULL;
    //int data_cache = -1;
    uint8_t *data_size_buf[2] = {0};
    int data_size_int = 0;
    int data_cache_fd1 = -1;
    int data_cache_fd2 = -1;
    char data[1] = {0};

    data_cache = (char*)malloc(2058);

    data_cache_fd1 = open(DTU_DATA_CACHE_FILE_NAME1, O_CREAT | O_RDWR);
    data_cache_fd2 = open(DTU_DATA_CACHE_FILE_NAME2, O_CREAT | O_RDWR);
    if(data_cache_fd1 != -1 && data_cache_fd2 != -1)
    {
        if(data_cache_flag == 1)
        {
            if(read(data_cache_fd1, data_size_buf, 2) > 0)
            {
                data_size_int = (uint8_t)data_size_buf[0] + ((uint8_t)data_size_buf[1])*256;
                read(data_cache_fd1, data_cache, data_size_int);

                while(read(data_cache_fd1, data, 1))
                {
                    write(data_cache_fd2,data,1);
                    fsync(data_cache_fd2);
                }
                unlink(DTU_DATA_CACHE_FILE_NAME1);
                data_cache_flag = 2;
            }
            else
            {
                return NULL;
            }
        }
        else
        {
            if(read(data_cache_fd2, data_size_buf, 2) > 0)
            {
                data_size_int = (uint8_t)data_size_buf[0] + ((uint8_t)data_size_buf[1])*256;
                read(data_cache_fd2, data_cache, data_size_int);

                while(read(data_cache_fd2, data, 1))
                {
                    write(data_cache_fd1,data,1);
                    fsync(data_cache_fd1);
                }
                unlink(DTU_DATA_CACHE_FILE_NAME2);
                data_cache_flag = 1;
            }
            else
            {
                return NULL;
            }
        }
    }
    else
    {
        close(data_cache_fd1);
        close(data_cache_fd2);
        LOG_E("open data cache file failed!");
        return NULL;
    }

    close(data_cache_fd1);
    close(data_cache_fd2);
    return data_cache;
}
